package priv.mill.bigdata.autompg;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
/**
 * 计算年度汽车的平均重量和马力
 * @author mill
 *
 */
public class AutoMpgMapper extends Mapper<LongWritable, Text, IntWritable, MyInfo> {

	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, IntWritable, MyInfo>.Context context)
			throws IOException, InterruptedException {
		try {
			String[] split = value.toString().replaceAll("\\s{2,}", "\t").split("\t");
			MyInfo myInfo = new MyInfo();
			myInfo.setHorsepower((int)Double.parseDouble(split[3]));//马力
			myInfo.setWeight((int)Double.parseDouble(split[4]));//重量
			context.write(new IntWritable(Integer.parseInt(split[6].substring(0, split[6].length()-1))), myInfo);
		} catch (NumberFormatException e) {
			
		}
	}

//	public static void main(String[] args) {
//		String str= "18.0   8.   307.0      130.0      3504.      12.0   70.  1.	\"chevrolet chevelle malibu\"\n" ;
//		String replaceAll = str.replaceAll("\\s{2,}", "\t");
//		System.out.println(replaceAll);
//	}
	
}
